package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.HydraulicSupportModel;
import com.aysafety.iotdataplatformserver.dao.HydraulicSupportDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 液压支架
@Component public class HydraulicSupportListener {

    private final String type = "hydraulicSupport";

    private final String tableName = "HydraulicSupportRealTime";

    private final String[] structure = new String[] {"hydraulicSupport"};

    private final String characteristicTableName = "HydraulicSupportCharacteristic";

    private final String[] characteristicStructure = new String[] {"Frequency", "Flicker"};

    private final String calWarningTableName = "CalWarningHydraulic";

    private final String[] calWarningStructure = new String[] {"warning"};
    @Autowired KsTreeDataListener ksTreeDataListener;
    @Autowired HydraulicSupportDao hydraulicSupportDao;
    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public boolean createCharacteristicTable() throws IOException {
        return HBaseDriver.createTable(this.characteristicTableName, this.characteristicStructure);
    }

    public boolean createCalWarningTable() throws IOException {
        return HBaseDriver.createTable(this.calWarningTableName, this.calWarningStructure);
    }

    public void start(Map<String, Object> result) {
        logger.info("HydraulicSupportListener.start,result=" + result);
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    HydraulicSupportModel hydraulicSupportData = this.transformData(listData);
                    this.save(hydraulicSupportData, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    HydraulicSupportModel hydraulicSupportData = this.transformData(listData);
                    this.save(hydraulicSupportData, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("HydraulicSupportListener:", e);
        }
    }

    public boolean save(HydraulicSupportModel hydraulicSupportData, String clientId) throws IOException {
        String timestamp = String.valueOf(hydraulicSupportData.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", hydraulicSupportData.getEntId() + "." + "hydraulicsupportrealtime");
        map.put("pk", "\'" + timestamp + ".:." + hydraulicSupportData.getDeviceId() + "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + hydraulicSupportData.getDeviceId() + "\'");
        map.put("entId", "\'" + hydraulicSupportData.getEntId() + "\'");
        map.put("devicePos", "\'" + hydraulicSupportData.getDevicePos() + "\'");
        map.put("workingFace", "\'" + hydraulicSupportData.getWorkingFace() + "\'");
        map.put("datetime", hydraulicSupportData.getDatetime().getTime());
        map.put("leftPressure", hydraulicSupportData.getLeftPressure());
        map.put("rightPressure", hydraulicSupportData.getRightPressure());
        map.put("totalStrain", hydraulicSupportData.getTotalStrain());
        map.put("maxValue", hydraulicSupportData.getMaxValue());
        map.put("minValue", hydraulicSupportData.getMinValue());
        hydraulicSupportDao.insertHydraulicSupportRealTime(map);
        ksTreeDataListener
            .generateTreeData(hydraulicSupportData.getEntId(), this.type, hydraulicSupportData.getWorkingFace(),
                hydraulicSupportData.getDeviceId(), hydraulicSupportData.getDatetime(),
                hydraulicSupportData.getDevicePos());

        return true;
    }

    public HydraulicSupportModel transformData(Map<String, Object> result) {
        HydraulicSupportModel hydraulicSupportData = new HydraulicSupportModel();
        hydraulicSupportData.setEntId(result.get("entId").toString());
        hydraulicSupportData.setDeviceId(result.get("deviceId").toString());
        if (null != result.get("leftPressure")) {
            hydraulicSupportData.setLeftPressure(Double.valueOf(result.get("leftPressure").toString()));
        } else {
            hydraulicSupportData.setLeftPressure(-1.0);
        }
        if (null != result.get("rightPressure")) {
            hydraulicSupportData.setRightPressure(Double.valueOf(result.get("rightPressure").toString()));
        } else {
            hydraulicSupportData.setLeftPressure(-1.0);
        }
        if (null != result.get("totalStrain")) {
            hydraulicSupportData.setTotalStrain(Double.valueOf(result.get("totalStrain").toString()));
        } else {
            hydraulicSupportData.setTotalStrain(-1.0);
        }
        if (null != result.get("maxValue")) {
            hydraulicSupportData.setMaxValue(Double.valueOf(result.get("maxValue").toString()));
        } else {
            hydraulicSupportData.setMaxValue(-1.0);
        }
        if (null != result.get("minValue")) {
            hydraulicSupportData.setMinValue(Double.valueOf(result.get("minValue").toString()));
        } else {
            hydraulicSupportData.setMinValue(-1.0);
        }
        hydraulicSupportData.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            hydraulicSupportData.setWorkingFace(result.get("workingFace").toString());
        } else {
            hydraulicSupportData.setWorkingFace("无数据");
        }
        if (null != result.get("devicePos")) {
            hydraulicSupportData.setDevicePos(result.get("devicePos").toString());
        } else {
            hydraulicSupportData.setDevicePos("无数据");
        }
        return hydraulicSupportData;
    }
}
